﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_TourCustomization_TourCustomization : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.initDataPaging();
        Alert1.HideMessage();
        if (!IsPostBack)
        {
            SetLabelLanguage();
            ShowMess();
            BindDataToGrid(1);
        }
    }
    private void SetLabelLanguage()
    {
        int nDefaultLang = Support.GetDefaultLanguage();
        btnAddnew.Text = MultipleLanguage.BTN_ADDNEW[nDefaultLang - 1];

        btnAddnew.Text = MultipleLanguage.BTN_ADDNEW[nDefaultLang - 1];
        btnCustomerCountry.Text = MultipleLanguage.CUSTOMERCOUNTRY[nDefaultLang - 1];
        btnHotelOption.Text = MultipleLanguage.HOTELOPTION[nDefaultLang - 1];
        btnMeal.Text = MultipleLanguage.MEAL[nDefaultLang - 1];
        btnPortOfArrival.Text = MultipleLanguage.PORTOFARRIVAL[nDefaultLang - 1];
        btnTourCustomization.Text = MultipleLanguage.TOURCUSTOMIZATION[nDefaultLang - 1];
        btnTypeOfTourCustomization.Text = MultipleLanguage.TYPEOFTOURCUSTOMIZATION[nDefaultLang - 1];
    }
    #region Paging & Message
    private void initDataPaging()
    {
        Pager1.PageSize = Config.PAGESIZE;
        Pager1.ShowFirstLast = true;
        Pager1.FirstClause = "Đầu";
        Pager1.LastClause = "Cuối";
        Pager1.NextClause = "Tiếp";
        Pager1.OfClause = "của";
        Pager1.PageClause = "Trang";
        Pager1.CompactedPageCount = 5;
        Pager1.PreviousClause = "Sau";
        Pager1.MaxSmartShortCutCount = 5;
        Pager1.NotCompactedPageCount = 5;
    }
    private void VisiblePaging(int intPagecount)
    {
        int nPageSize = 0;
        nPageSize = Config.PAGESIZE;
        if (intPagecount > nPageSize)
            Pager1.Visible = true;
        else
            Pager1.Visible = false;
    }
    public void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndx;
        BindDataToGrid(currnetPageIndx);
    }
    private void ShowMess()
    {
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        lblWarning.Text = MessagesAlert.SEARCH_NO_ITEM[nDefaultLang];
        switch (nMessage)
        {
            case 1:
                Alert1.ShowMessage(1, MessagesAlert.INSERT_SUCCESSFULL[nDefaultLang]);
                break;
            case 2:
                Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLang]);
                break;
            default:
                Alert1.HideMessage();
                break;
        }
    }
    #endregion

    #region Click Events
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURCUSTOMIZATION);
    }
    #endregion

    #region BindDataToGridView
    private void BindDataToGrid(int CurrentPageIndex)
    {
        TourCustomization objTourCustomization = new TourCustomization();
        DataTable dtbTourCustomization = new DataTable();
        int nPageCount = 0;
        dtbTourCustomization = objTourCustomization.Search(0, 0, CurrentPageIndex, Config.PAGESIZE, ref nPageCount);
        dtgTourCustomization.DataSource = dtbTourCustomization;
        dtgTourCustomization.DataBind();
        int nDefaultLang = Support.GetDefaultLanguage();
        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefaultLang - 1] + nPageCount + MultipleLanguage.LBL_FOUND[nDefaultLang - 1];

        Pager1.ItemCount = nPageCount;
        VisiblePaging(nPageCount);

        if (nPageCount > 0)
            tblWarning.Visible = false;
        else
            tblWarning.Visible = true;
    }
    
    protected void dtgTourCustomization_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        TourCustomization objTourCustomization = new TourCustomization();
        switch (strCommand)
        {
            case "Delete":
                int nDelete = objTourCustomization.DeleteById(nID);
                if (nDelete > 0)
                    Alert1.ShowMessage(1, MessagesAlert.DELETE_SUCCESSFULL[nDefaultLang]);
                else
                    Alert1.ShowMessage(2, MessagesAlert.DELETE_UNSUCCESSFULL[nDefaultLang]);
                BindDataToGrid(1);
                //BindDataToGrid(Pager1.CurrentIndex);
                break;

            case "Edit":
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURCUSTOMIZATION_ADD + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;
        }
    }
    protected void dtgTourCustomization_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        int nDefaultLang = Support.GetDefaultLanguage();
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblFullName = (Label)e.Item.FindControl("lblFullName");
            Label lblEmail = (Label)e.Item.FindControl("lblEmail");
            Label lblTelephone = (Label)e.Item.FindControl("lblTelephone");
            Label lblBookingDate = (Label)e.Item.FindControl("lblBookingDate");

            Label lnkUnread = (Label)e.Item.FindControl("lnkUnread");
            Label lnkRead = (Label)e.Item.FindControl("lnkRead");
            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");

            lblFullName.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "FullName"));
            lblEmail.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Email"));
            lblTelephone.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Telephone"));
            lblBookingDate.Text = ConvertData.ConvertIntToDatetimeString(ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "BookingDate")));
            int nStatus = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Status"));
            if (nStatus > 0)
            {
                // read
                lnkUnread.Visible = false;
                lnkRead.Visible = true;
            }
            else
            {
                lnkUnread.Visible = true;
                lnkRead.Visible = false;
            }
            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TourCustomizationID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_ALERT[nDefaultLang - 1]);
            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TourCustomizationID"));
        }
    }

    protected void btnAddnew_Click1(object sender, EventArgs e)
    {
        string sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURCUSTOMIZATION;
        Response.Redirect(sLink);
    }
    protected void btnCustomerCountry_Click(object sender, EventArgs e)
    {
        string sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_CUSTOMERCOUNTRY;
        Response.Redirect(sLink);
    }
    protected void btnHotelOption_Click(object sender, EventArgs e)
    {
        string sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_HOTELOPTION;
        Response.Redirect(sLink);
    }
    protected void btnMeal_Click(object sender, EventArgs e)
    {
        string sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_MEAL;
        Response.Redirect(sLink);
    }
    protected void btnPortOfArrival_Click(object sender, EventArgs e)
    {
        string sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_PORTOFARRIVAL;
        Response.Redirect(sLink);
    }
    protected void btnTourCustomization_Click(object sender, EventArgs e)
    {
        string sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURCUSTOMIZATION;
        Response.Redirect(sLink);
    }
    protected void btnTypeOfTourCustomization_Click(object sender, EventArgs e)
    {
        string sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TYPEOFTOURCUSTOMIZATION;
        Response.Redirect(sLink);
    }
    #endregion
}